<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>疫苗数据管理</el-breadcrumb-item>
      <el-breadcrumb-item>世界接种数据</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 白底 -->
    <el-card class="box-card">
      <!-- 搜索&添加数据 -->
      <el-row :gutter="20">
        <!-- 搜索模块开始 -->
        <el-col :span="8">
          <el-input
            placeholder="搜索地区"
            class="input-with-select"
            v-model="queryInfo.query"
            clearable
            @clear="searchworldvaccList"
            @change="searchworldvaccList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="searchworldvaccList"
            ></el-button>
          </el-input>
        </el-col>
        <!-- 搜索模块结束 -->
        <!-- 添加数据按钮 -->
        <el-col :span="4">
          <el-button type="primary" @click="addDialogVisible = true"
            >添加数据</el-button
          >
        </el-col>
      </el-row>
      <!-- 表格模块开始 -->
      <el-table
        v-loading="loading"
        element-loading-text="正在加载捏"
        :data="tableData"
        style="width: 100%"
        :default-sort="{ prop: 'inoculateCount', order: 'descending' }"
        height="660"
      >
        <el-table-column type="index" width="50"> </el-table-column>
        <el-table-column prop="countryName" label="地区" width="150">
        </el-table-column>
        <el-table-column prop="dateTime" sortable label="更新时间" width="200">
          <template slot-scope="scope">
            <i class="el-icon-time"></i>
            {{ scope.row.dateTime | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column prop="vaccineType" label="疫苗种类" width="500">
        </el-table-column>
        <el-table-column
          prop="inoculateCount"
          sortable
          label="接种数"
          width="200"
        >
        </el-table-column>
        <el-table-column
          prop="inoculateCount100"
          sortable
          label="接种数/百人"
          width="200"
        >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.inoculateCount100 > 250" type="success">
              {{ scope.row.inoculateCount100 }}</el-tag
            >
            <el-tag
              v-if="
                scope.row.inoculateCount100 <= 250 &&
                scope.row.inoculateCount100 > 150
              "
            >
              {{ scope.row.inoculateCount100 }}</el-tag
            >
            <el-tag
              v-if="
                scope.row.inoculateCount100 <= 150 &&
                scope.row.inoculateCount100 > 80
              "
              type="warning"
            >
              {{ scope.row.inoculateCount100 }}</el-tag
            >
            <el-tag
              v-if="
                scope.row.inoculateCount100 > 0 &&
                scope.row.inoculateCount100 < 80
              "
              type="danger"
            >
              {{ scope.row.inoculateCount100 }}</el-tag
            >
          </template>
        </el-table-column>
        <!-- 操作按钮列 -->
        <el-table-column label="操作">
          <template slot-scope="scope">
            <!-- 编辑按钮 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEditworldvaccForm(scope.row.id)"
            ></el-button>
            <!-- 删除按钮 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="deleteworldvacc(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 表格模块结束 -->
      <!-- 翻页插件开始 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[10, 50, 100, 1000]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageTotal"
      >
      </el-pagination>
      <!-- 翻页插件结束 -->
    </el-card>
    <!-- 添加数据弹框 -->
    <el-dialog
      title="添加数据"
      :visible.sync="addDialogVisible"
      width="30%"
      @close="addworldvaccFormClose"
    >
      <el-form
        ref="addworldvaccRef"
        :model="addworldvaccFormData"
        label-width="80px"
      >
        <el-form-item label="地区" prop="countryName">
          <el-input v-model="addworldvaccFormData.countryName"></el-input>
        </el-form-item>
        <el-form-item label="时间" prop="dateTime">
          <el-date-picker
            v-model="addworldvaccFormData.dateTime"
            type="datetime"
            placeholder="选择更新时间时间"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="疫苗种类" prop="vaccineType">
          <el-input v-model="addworldvaccFormData.vaccineType"></el-input>
        </el-form-item>
        <el-form-item label="接种数" prop="inoculateCount">
          <el-input
            v-model="addworldvaccFormData.inoculateCount"
            type="number"
          ></el-input>
        </el-form-item>
        <el-form-item label="接种数/百人" prop="inoculateCount100">
          <el-input v-model="addworldvaccFormData.inoculateCount100"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addworldvacc">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 编辑数据弹框 -->
    <el-dialog title="编辑数据" :visible.sync="editDialogVisible" width="30%">
      <el-form
        ref="editworldvaccRef"
        :model="editworldvaccFormData"
        label-width="80px"
      >
        <el-form-item label="地区" prop="countryName">
          <el-input v-model="editworldvaccFormData.countryName"></el-input>
        </el-form-item>
        <el-form-item label="时间" prop="dateTime">
          <el-date-picker
            v-model="editworldvaccFormData.dateTime"
            type="datetime"
            placeholder="选择更新时间时间"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="疫苗种类" prop="vaccineType">
          <el-input v-model="editworldvaccFormData.vaccineType"></el-input>
        </el-form-item>
        <el-form-item label="接种数" prop="inoculateCount">
          <el-input
            v-model="editworldvaccFormData.inoculateCount"
            type="number"
          ></el-input>
        </el-form-item>
        <el-form-item label="接种数/百人" prop="inoculateCount100">
          <el-input
            v-model="editworldvaccFormData.inoculateCount100"
          ></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editworldvacc">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      // 表格数据
      tableData: [
        {
          id: 2,
          countryName: "中国",
          dateTime: "2022-03-15T16:00:00.000Z",
          vaccineType: "1",
          inoculateCount: 3209041000,
          inoculateCount100: 222.95,
        },
      ],
      queryInfo: {
        query: "", //搜索条件
        pagenum: 1, //当前显示第几页
        pagesize: 1000, //每一页显示多少条
      },
      // 表格载入状态,如果还在加载就是true
      loading: true,
      // 总条数
      pageTotal: 0,
      //添加对话框显示隐藏
      addDialogVisible: false,
      //编辑对话框显示隐藏
      editDialogVisible: false,
      // 添加用户数据
      addworldvaccFormData: {
        countryName: "中国1",
        dateTime: "2022-05-19",
        vaccineType: "1",
        inoculateCount: 100000,
        inoculateCount100: 100,
      },
      // 编辑用户数据
      editworldvaccFormData: {
        countryName: "中国1",
        dateTime: "2022-05-19",
        vaccineType: "1",
        inoculateCount: 100000,
        inoculateCount100: 100,
      },
    };
  },
  // 进入页面获取数据
  created() {
    this.getworldvaccList();
  },
  methods: {
    // 搜索功能的函数
    searchworldvaccList() {
      this.queryInfo.pagenum = 1;
      this.getworldvaccList();
    },
    // 获取数据的函数
    async getworldvaccList() {
      this.loading = true;
      let { data: res } = await this.$http.get("worldvacc", {
        params: this.queryInfo,
      });
      if (res.meta.status !== 200) {
        this.$message.error(res.meta.msg);
      } else {
        this.tableData = res.data.vaccworld;
        this.pageTotal = res.data.total;
      }
      this.loading = false;
    },
    // 这两条是点击下面分页条条的函数
    handleSizeChange(params) {
      this.queryInfo.pagesize = params;
      this.getworldvaccList();
    },
    handleCurrentChange(params) {
      this.queryInfo.pagenum = params;
      this.getworldvaccList();
    },
    //展示编辑数据对话框的函数,并通过id将信息填入对话框
    async showEditworldvaccForm(id) {
      this.editDialogVisible = true;
      let { data: res } = await this.$http.get(`worldvacc/${id}`);
      if (res.meta.status !== 200) {
        this.$message.error(res.meta.msg);
      } else {
        this.editworldvaccFormData = res.data;
      }
    },
    // 编辑接种信息函数
    editworldvacc() {
      this.$refs.editworldvaccRef.validate(async (valid) => {
        //向后端发送请求
        if (valid) {
          let { data: res } = await this.$http.put(
            `worldvacc/${this.editworldvaccFormData.id}`,
            this.editworldvaccFormData
          );
          //判断状态码
          if (res.meta.status !== 200) {
            this.$message.error(res.meta.msg);
          } else {
            this.$message.success(res.meta.msg);
            this.editDialogVisible = false;
            this.getworldvaccList();
          }
        } else {
          return;
        }
      });
    },
    // 这个是关闭对话框时清除输入内容的函数
    addworldvaccFormClose() {
      this.$refs.addworldvaccRef.resetFields();
    },
    //删除疫苗数据函数
    async deleteworldvacc(id) {
      //这里开始是确认删除对话框
      let confirmResult = await this.$confirm(
        "此操作将永久本条数据, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch(() => {
        this.$message({
          type: "info",
          message: "已取消删除",
        });
      });
      //这里开始是向后端发送删除请求
      if (confirmResult == "confirm") {
        let { data: res } = await this.$http.delete("worldvacc/" + id);
        // 判断状态码
        if (res.meta.status !== 200) {
          this.$message.error(res.meta.msg);
        } else {
          this.$message.success(res.meta.msg);
          this.getworldvaccList();
        }
      }
    },
    //添加数据的函数
    addworldvacc() {
      this.$refs.addworldvaccRef.validate(async (valid) => {
        if (valid) {
          // 向后端发送添加请求
          let { data: res } = await this.$http.post(
            "worldvacc/add",
            this.addworldvaccFormData
          );
          // 判断状态码
          if (res.meta.status !== 200) {
            this.$message.error(res.meta.msg);
          } else {
            this.$message.success(res.meta.msg);
            this.addDialogVisible = false;
            this.getworldvaccList();
          }
        } else {
          return;
        }
      });
    },
  },
};
</script>
<style lang="less" scoped></style>